package com.qitianyong.sgkusbcamera;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.gkusbsdk.GKUSBCommand;
import com.gkusbsdk.GKUSBSDK;
import com.gkusbsdk.GKUSBTime;
import com.ligo.log.util.ZyLog;
import com.qitianyong.selfclass.CustomApp;
import com.qitianyong.selfclass.FindDeviceThread;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.selfclass.LogUtils;
import com.qitianyong.selfclass.PublicMethod;

import static com.qitianyong.selfclass.CustomApp.handler;
import static com.qitianyong.selfclass.FindDeviceThread.OPEN_APP;

public class USBPermissionRequestResultReceiver extends BroadcastReceiver {
    public static final String ACTION_USB_PERMISSION = "com.qitianyong.USB_PERMISSION";

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (null != usbDevice) {
                        ZyLog.d("initDevUsbDevice");
                        initDevUsbDevice(usbDevice);
                    }
                }
            }
        }
    }

    public static void initDevUsbDevice(UsbDevice usbDevice) {
        UsbManager usbManager = (UsbManager) CustomApp.customApp.getSystemService(Context.USB_SERVICE);
        UsbDeviceConnection connection = usbManager.openDevice(usbDevice);
        if (connection == null) {
            return;
        }
        int fd = connection.getFileDescriptor();
        String name = usbDevice.getDeviceName();
        final String[] v = name.split("/");
        int busnum = 0;
        int devnum = 0;
        String usbfs = null;
        ZyLog.d("getDeviceName =" + name);
        busnum = Integer.parseInt(v[v.length - 2]);
        devnum = Integer.parseInt(v[v.length - 1]);
        if ((v.length > 2)) {
            final StringBuilder sb = new StringBuilder(v[0]);
            for (int i = 1; i < v.length - 2; i++)
                sb.append("/").append(v[i]);
            usbfs = sb.toString();
        }
        int time = 10;
        while (time-- > 0) {
            long hDevice = GKUSBSDK.GKUSB_CreateBusNodeCam(fd, usbDevice.getVendorId(), usbDevice.getProductId(), busnum, devnum, usbfs);
            ZyLog.d("GKUSB_CreateBusNodeCam =" + hDevice);
            if (hDevice != 0) {
                PublicMethod.sleep(500);
                GKUSBTime usbTime = PublicMethod.CurrentTime2USBTime();
                int ret = GKUSBSDK.GKUSB_SetParam(hDevice, GKUSBCommand.CMD_SYNC_TIME, usbTime);
                LogUtils.addLog("Sync time result" + ret);
                if (ret == 0) {
                    PublicMethod.sleep(500);
                    ret = GKUSBSDK.GKUSB_SetProtocol(hDevice, GKUSBCommand.PROTOCOL_TYPE.TYPE_NEW);
                    if (ret == 1) {  // 设置成功
                        GKDevice.__hDevice = hDevice;
                        GKDevice.usbConnection = connection;
                        if (FindDeviceThread.getInstance().intent == OPEN_APP) {
                            handler.sendEmptyMessageDelayed(0, 500);
                        }
                        Log.i("GKDevice", "-----SetProtocol ------" + ret);
                        break;
                    } else {
                        GKUSBSDK.GKUSB_Destroy(hDevice);
                    }
                } else {
                    GKUSBSDK.GKUSB_Destroy(hDevice);
                }
            }
            PublicMethod.sleep(100);
        }
        if (GKDevice.__hDevice == 0) {
            connection.close();
        }

    }
}
